package com.ruoyi.project.gxgw_modules.discountManager.controller;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.gxgw_modules.discountManager.domain.Yhq;
import com.ruoyi.project.gxgw_modules.discountManager.domain.YhqType;
import com.ruoyi.project.gxgw_modules.discountManager.service.IYhqTypeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 优惠券类型 信息操作处理
 *
 * @author ruoyi
 * @date 2019-01-18
 */
@Controller
@RequestMapping("/gxgw_modules/yhqType")
public class YhqTypeController extends BaseController {
    private String prefix = "gxgw_modules/yhqType";

    @Autowired
    private IYhqTypeService yhqTypeService;

    @RequiresPermissions("gxgw_modules:yhqType:view")
    @GetMapping()
    public String yhqType() {
        return prefix + "/yhqType";
    }

    /**
     * 查询优惠券类型列表
     */
    @RequiresPermissions("gxgw_modules:yhqType:list")
    @GetMapping("/list")
    @ResponseBody
    public List<YhqType> list(YhqType yhqType) {
        List<YhqType> yhqTypeList = yhqTypeService.selectYhqTypeList(yhqType);
        return yhqTypeList;
    }
//    @PostMapping("/list")
/*    public TableDataInfo list(YhqType yhqType) {
        startPage();
        List<YhqType> list = yhqTypeService.selectYhqTypeList(yhqType);
        return getDataTable(list);
    }*/



    /**
     * 导出优惠券类型列表
     */
    @RequiresPermissions("gxgw_modules:yhqType:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(YhqType yhqType) {
        List<YhqType> list = yhqTypeService.selectYhqTypeList(yhqType);
        ExcelUtil<YhqType> util = new ExcelUtil<YhqType>(YhqType.class);
        return util.exportExcel(list, "yhqType");
    }

    /**
     * 新增优惠券类型
     */
//    @GetMapping("/add")
//    public String add() {
//        return prefix + "/add";
//    }

    /**
     * 新增优惠券类型
     */
    @GetMapping("/add/{fId}")
    public String add(@PathVariable("fId") Integer fId, ModelMap mmap) {
        mmap.put("yhqType", yhqTypeService.selectYhqTypeById(fId));
        return prefix + "/add";
    }

    /**
     * 新增保存优惠券类型
     */
    @RequiresPermissions("gxgw_modules:yhqType:add")
    @Log(title = "优惠券类型", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(YhqType yhqType) {
        if (yhqType.getMenu() == null) {
            YhqType fYhqType = this.yhqTypeService.selectYhqTypeById(yhqType.getFId());
            int fMenu = fYhqType.getMenu();
            yhqType.setMenu(fMenu + 1);
        }

        return toAjax(yhqTypeService.insertYhqType(yhqType));
    }

    /**
     * 修改优惠券类型
     */
    @GetMapping("/edit/{typeId}")
    public String edit(@PathVariable("typeId") Integer typeId, ModelMap mmap) {
        YhqType yhqType = yhqTypeService.selectYhqTypeById(typeId);
        if (StringUtils.isNotNull(yhqType) && 1 == typeId) {
            yhqType.setfName("无");
        } else {
            YhqType fYhqType = yhqTypeService.selectYhqTypeById(yhqType.getFId());
            yhqType.setfName(fYhqType.getTypeName());
//            System.out.println("输出--------" + yhqType.toString() + "fName" + yhqType.getfName());
        }

        mmap.put("yhqType", yhqType);
        return prefix + "/edit";
    }

    /**
     * 修改保存优惠券类型
     */
    @RequiresPermissions("gxgw_modules:yhqType:edit")
    @Log(title = "优惠券类型", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(YhqType yhqType) {
        YhqType fYhqType = this.yhqTypeService.selectYhqTypeById(yhqType.getFId());
        int fMenu = fYhqType.getMenu();
        yhqType.setMenu(fMenu + 1);

        return toAjax(yhqTypeService.updateYhqType(yhqType));
    }

/*    *//**
     * 删除优惠券类型
     *//*
    @RequiresPermissions("gxgw_modules:yhqType:remove")
    @Log(title = "优惠券类型", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
//        return toAjax(yhqTypeService.deleteYhqTypeByIds(ids));
        return toAjax(yhqTypeService.deleteYhqTypeDelFlagByIds(ids));
    }*/
    /**
     * 删除
     */
    @Log(title = "优惠券类型", businessType = BusinessType.DELETE)
    @RequiresPermissions("gxgw_modules:yhqType:remove")
    @PostMapping("/remove/{typeId}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("typeId") Integer typeId) {
        if (yhqTypeService.selectYhqTypeCount(typeId) > 0) {
            return error(1, "存在下级类型,不允许删除");
        }
        if (yhqTypeService.checkTypeExistYhq(typeId)) {
            return error(1, "该类型存在优惠券,不允许删除");
        }
        return toAjax(yhqTypeService.deleteYhqTypeById(typeId));
    }



	/**
	 * 校验优惠券类型名称
	 */
	@PostMapping("/checkYhqTypeNameUnique")
	@ResponseBody
	public String checkYhqTypeNameUnique(YhqType yhqType) {
		return yhqTypeService.checkYhqTypeNameUnique(yhqType);
	}

	/**
	 * 选择优惠类型树
	 */
	@GetMapping("/selectTypeTree/{typeId}")
	public String selectDeptTree(@PathVariable("typeId") Integer typeId, ModelMap mmap) {
		mmap.put("yhqType", yhqTypeService.selectYhqTypeById(typeId));
		return prefix + "/tree";
	}

	/**
	 * 加载优惠类型列表树
	 */
	@GetMapping("/treeData")
	@ResponseBody
	public List<Map<String, Object>> treeData() {
		List<Map<String, Object>> tree = yhqTypeService.selectTypeTree(new YhqType());
		return tree;
	}

	/**
	 * 加载优惠券优惠类型（数据权限）列表树
	 */
	@GetMapping("/yhqYhqTypeTreeData")
	@ResponseBody
	public List<Map<String, Object>> yhqTypeTreeData(Yhq yhq) {
//		List<Map<String, Object>> tree = yhqTypeService.roleDeptTreeData(yhq);
//		return tree;
        return null;
	}

}
